package com.pengtu.service.asset;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.pengtu.dao.Page;
import com.pengtu.dao.QueryFilter;
import com.pengtu.dao.asset.car.InformationDao;
import com.pengtu.dao.asset.car.RecordDao;
import com.pengtu.entity.asset.car.Information;
import com.pengtu.entity.asset.car.Record;

@Component
@Transactional
public class CarManager {


	private InformationDao carInformationDao;
	private RecordDao carRecordDao;

	// CarInformation Manager

	/**
	 * 
	 * @param car
	 *            实体对象
	 */
	public void saveCar(Information car) {
		if (StringUtils.isEmpty(car.getId())) {
			carInformationDao.save(car);
		} else {
			carInformationDao.update(car);
		}
	}

	/**
	 * 验证车牌号是否重复
	 * @param oldCarLicen 
	 * @param newCarLicen 
	 * @return
	 */
	public boolean checkLienNum(String newCarLicen, String oldCarLicen) {
		// TODO Auto-generated method stub
		return carInformationDao.isPropertyUnique("licenseplateNumber", newCarLicen, oldCarLicen) ;
	}
	public boolean checkSindex(String newSindex, String oldSindex) {
		// TODO Auto-generated method stub
		return carInformationDao.isPropertyUnique("sindex", newSindex, oldSindex) ;
	}

	public boolean checkCode(String newCode, String oldCode) {
		// TODO Auto-generated method stub
		return carInformationDao.isPropertyUnique("code", newCode, newCode) ;
	}

	public boolean checkEngineNo(String newEngineNo, String oldEngineNo) {
		// TODO Auto-generated method stub
		return carInformationDao.isPropertyUnique("engineNo", newEngineNo, oldEngineNo) ;
	}

	public boolean checkFrameNo(String newFrameNo, String oldFrameNo) {
		// TODO Auto-generated method stub
		return carInformationDao.isPropertyUnique("frameNo", newFrameNo, oldFrameNo) ;
	}
	
	/**
	 * 
	 * @param carId
	 *            删除对象的Id
	 */
	public void deleteCar(String carId) {

		carInformationDao.delete(carId);
	}

	/**
	 * 修改车辆信息
	 * 
	 * @param car
	 */
	public void updateCar(Information car) {
		carInformationDao.update(car);
	}

	/**
	 * 查询单条数据
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public Information findById(String carId) {

		return carInformationDao.get(carId);
	}

	/**
	 * 分页显示
	 * 
	 * @param page
	 * @param car
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<Information> findAllCar(Page<Information> page) {

		return carInformationDao.getAll(page);
	}

	/**
	 * 加模糊查询的分页显示
	 * 
	 * @return
	 */
	public Page<Information> searchPageCar(final Page<Information> page,
			final QueryFilter queryFilter) {
		return carInformationDao.findPage(page, queryFilter);
	}

	/**
	 * 添加用车记录
	 */
	public void addCarRe(Record carRe) {
		if (StringUtils.isEmpty(carRe.getId())) {
			carRecordDao.save(carRe);
		} else {
			carRecordDao.update(carRe);
		}

	}

	/**
	 * 修改用车记录
	 * 
	 * @param carRe
	 */
	public void updateCarRe(Record carRe) {
		carRecordDao.update(carRe);
	}

	/**
	 * 将要删除的记录的删除标记改为1
	 * 
	 * @param catReId
	 */
	public void deleteCarRe(String catReId) {
		Record carRe = carRecordDao.get(catReId);
		carRe.setDelFlag("1");
		carRecordDao.update(carRe);
	}

	/**
	 * 根据carReId获取单条数据
	 * 
	 * @param carReId
	 * @return
	 */
	@Transactional(readOnly = true)
	public Record findCarRe(String carReId) {
		return carRecordDao.get(carReId);
	}

	/**
	 * 分页显示
	 * 
	 * @param page
	 * @return
	 */
	@Transactional(readOnly = true)
	public Page<Record> findAllCarRe(Page<Record> page) {
		return carRecordDao.getAll(page);
	}

	
	
	/**
	 * 
	 * getCodeList:
	 * 适用:所有的码表列表
	 * @return 
	 * @exception 
	 * @since  1.0.0
	 */
	@Transactional(readOnly = true)
	public Page<Record> searchPageCarRe(final Page<Record> page, final QueryFilter queryFilter){
		return carRecordDao.findPage(page, queryFilter);
	}
	/**
	 * 查询单位信息
	 * 
	 * @return
	 */
	@Transactional(readOnly = true)
	public List<Information> getCarInfo() {
		return carInformationDao.find();
	}

	@Transactional(readOnly = true)
	public Information getCarInfo(String id) {
		return carInformationDao.get(id);
	}

	@Autowired
	public void setCarInformationDao(InformationDao carInformationDao) {
		this.carInformationDao = carInformationDao;
	}

	@Autowired
	public void setCarRecordDao(RecordDao carRecordDao) {
		this.carRecordDao = carRecordDao;
	}
	
	
	/**得到所有的车辆使用记录*/
	public List<Record> getRecordCar() {
		return carRecordDao.getAll();
	}

	public List<Record> findCarReByCarNum(String licenseplateNumber) {
		// TODO Auto-generated method stub
		return carRecordDao.findBy("choicecar", licenseplateNumber);
	}

	public Page<Information> findNoUserCar(Page<Information>  page, String startDate, String endDate) {
		// TODO Auto-generated method stub
		return carInformationDao.findNoUserCar(page,startDate,endDate);
	}

	public List<Record> getSelectListToJson(QueryFilter queryFilter) {
		// TODO Auto-generated method stub
		return carRecordDao.find(queryFilter);
	}

}
